package com.xxx.system.controller;


import com.github.pagehelper.PageInfo;
import com.xxx.base.utils.R;
import com.xxx.system.annotation.PreAuthorize;
import com.xxx.system.domain.Employee;
import com.xxx.system.query.EmployeeQuery;
import com.xxx.system.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //默认返回json格式，前后端分离
@RequestMapping("/employee")
@Api(tags = "员工接口类")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    //所有controller的接口  都是Restful风格
//查询所有
//localhost:8080/employee  ---Get请求方式
    @GetMapping
    @ApiOperation(value = "查询所有员工")
    @PreAuthorize(name = "查询所有员工",sn="employee:findAll",descs = "查询所有员工")//员工使用自定义注解
    public R<List<Employee>> findAll() {
        List<Employee> list = employeeService.findAll();
        return R.ok(list);
    }

    //根据id查询单个Employee
//localhost:8080/employee/{id}  ---Get请求方式
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个员工")
    @PreAuthorize(name = "查询单个员工",sn="employee:get",descs = "查询单个员工")//员工使用自定义注解
    public R<Employee> findOne(@PathVariable("id") Long id) {
        Employee employee = employeeService.findOne(id);
        return R.ok(employee);
    }

    //新增 或者 修改
    //前端：Put：http://localhost:8080/employee/  json对象 - 请求体
    //@RequestBody Employee sysUse 使用Employee对象去接受请求体中的数据，要求属性名一致
    @PutMapping
    @ApiOperation(value = "新增or修改员工")
    @PreAuthorize(name = "新增or修改员工",sn="employee:addOrUpdate",descs = "新增or修改员工")//员工使用自定义注解
    public R<Void> addOrUpdate(@RequestBody Employee employee) {
        try {
            //获取请求参数的id
            Long id = employee.getId();
            if (id == null) {//新增没有传递id
                employeeService.addEmployee(employee);
            } else {
                employeeService.updateEmployee(employee);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("网络异常，请稍后重试!");
        }
        return R.ok();
    }

    //删除
    //前端：Delete：http://localhost:8080/employee/2
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个员工")
    @PreAuthorize(name = "删除单个员工",sn="employee:deleteById",descs = "删除单个员工")//员工使用自定义注解
    public R<Void> deleteById(@PathVariable("id") Long id) {
        try {
            employeeService.deteleById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("网络异常，请稍后重试!");
        }
        return R.ok();
    }

    //分页接口+高级查询(根据条件查询后也需要分页展示)
    @PostMapping
    @ApiOperation(value = "高级查询&分页")
    @PreAuthorize(name = "高级查询&分页",sn="employee:queryPage",descs = "高级查询&分页")//员工使用自定义注解
    public PageInfo<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery) {
        List<Employee> employeeList = employeeService.queryPage(employeeQuery);
        //返回的是封装好的分页(总数,list)
        return new PageInfo<Employee>(employeeList);
    }

    //批量删除，接收前端id数组
    @PatchMapping
    @ApiOperation(value = "批量删除")
    @PreAuthorize(name = "批量删除",sn="employee:deleteEmployeeByIds",descs = "批量删除")//员工使用自定义注解
    public R<Void> deleteEmployeeByIds(@RequestBody Long[] ids) {
        try {
            employeeService.deleteEmployeeByIds(ids);
        } catch (Exception e) {
            e.printStackTrace();
            //返回的json格式  {"success":false,"message":"错误信息"}
            return R.fail("网络异常，请稍后重试!");
        }
        return R.ok();
    }
}